package com.sqlcheck.util;

import com.sqlcheck.entity.ScanResult;
import com.sqlcheck.dto.response.ScanResultVO;

/**
 * 枚举转换工具类，确保类型安全的枚举转换
 */
public class EnumConverter {
    
    /**
     * 将字符串转换为ScanResult.IssueType枚举
     * 支持兼容性映射，FUNCTION和DATATYPE类别映射到COMPATIBILITY_WARNING
     */
    public static ScanResult.IssueType parseIssueType(String category) {
        if (category == null || category.trim().isEmpty()) {
            // 返回默认值而不是null，避免数据库枚举解析错误
            return ScanResult.IssueType.COMPATIBILITY_WARNING;
        }
        
        String upperCategory = category.trim().toUpperCase();
        switch (upperCategory) {
            case "SYNTAX":
                return ScanResult.IssueType.SYNTAX_ERROR;
            case "FUNCTION":
            case "DATATYPE":
            case "COMPATIBILITY":
                return ScanResult.IssueType.COMPATIBILITY_WARNING;
            case "PERFORMANCE":
                return ScanResult.IssueType.PERFORMANCE_ISSUE;
            case "SECURITY":
                return ScanResult.IssueType.SECURITY_RISK;
            // 直接匹配枚举名称
            case "SYNTAX_ERROR":
                return ScanResult.IssueType.SYNTAX_ERROR;
            case "FUNCTION_ISSUE":
            case "DATATYPE_ISSUE":
            case "COMPATIBILITY_WARNING":
                return ScanResult.IssueType.COMPATIBILITY_WARNING;
            case "PERFORMANCE_ISSUE":
                return ScanResult.IssueType.PERFORMANCE_ISSUE;
            case "SECURITY_RISK":
                return ScanResult.IssueType.SECURITY_RISK;
            default:
                // 默认映射为兼容性警告
                return ScanResult.IssueType.COMPATIBILITY_WARNING;
        }
    }
    
    /**
     * 将字符串转换为ScanResult.Severity枚举
     */
    public static ScanResult.Severity parseSeverity(String severity) {
        if (severity == null || severity.trim().isEmpty()) {
            return ScanResult.Severity.MEDIUM; // 默认为中等严重程度
        }
        
        String upperSeverity = severity.trim().toUpperCase();
        switch (upperSeverity) {
            case "HIGH":
                return ScanResult.Severity.HIGH;
            case "MEDIUM":
                return ScanResult.Severity.MEDIUM;
            case "LOW":
                return ScanResult.Severity.LOW;
            case "INFO":
                return ScanResult.Severity.INFO;
            default:
                return ScanResult.Severity.MEDIUM;
        }
    }
    
    /**
     * 将ScanResult.IssueType转换为ScanResultVO.IssueTypeEnum
     */
    public static ScanResultVO.IssueTypeEnum toVOIssueType(ScanResult.IssueType issueType) {
        if (issueType == null) {
            return null;
        }
        return ScanResultVO.IssueTypeEnum.valueOf(issueType.name());
    }
    
    /**
     * 将ScanResult.Severity转换为ScanResultVO.SeverityEnum
     */
    public static ScanResultVO.SeverityEnum toVOSeverity(ScanResult.Severity severity) {
        if (severity == null) {
            return null;
        }
        return ScanResultVO.SeverityEnum.valueOf(severity.name());
    }
    
    /**
     * 将分析引擎字符串转换为AnalysisEngineEnum
     */
    public static ScanResultVO.AnalysisEngineEnum parseAnalysisEngine(String engineStr) {
        if (engineStr == null || engineStr.trim().isEmpty()) {
            return ScanResultVO.AnalysisEngineEnum.RULE;
        }
        
        String upperEngine = engineStr.trim().toUpperCase();
        switch (upperEngine) {
            case "RULE":
                return ScanResultVO.AnalysisEngineEnum.RULE;
            case "AI":
            case "RULE_WITH_AI_ENHANCEMENT":
                return ScanResultVO.AnalysisEngineEnum.RULE_WITH_AI_ENHANCEMENT;
            default:
                return ScanResultVO.AnalysisEngineEnum.RULE;
        }
    }
    
    /**
     * 验证枚举值是否有效
     */
    public static boolean isValidIssueType(String category) {
        try {
            parseIssueType(category);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    
    /**
     * 验证严重程度是否有效
     */
    public static boolean isValidSeverity(String severity) {
        try {
            parseSeverity(severity);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}